<template>
  <div class="detail">
    <detail-nav-bar class="nav-bar" @titleClick="titleClick" ref="nav"></detail-nav-bar>
    <scroll class="scroll" ref="scroll" @scroll="scroll" :probeType="3">
      <detail-swiper :topimages="topimages"></detail-swiper>
      <detail-goods-info :goods="goods"></detail-goods-info>
      <detail-shop-info :shop="shop"></detail-shop-info>
      <detail-image-info :detailimages="detailimages"/>
      <detail-params :params="params" ref="params"></detail-params>
      <detail-comment :comment="comment" ref="comment"></detail-comment>
      <goods-list :goodslist="recommend" ref="recommend"></goods-list>
    </scroll>
    <back-top @click.native="backClick" v-show="iscurrent"></back-top>
    <detail-bottom-bar @addToCart="addToCart"></detail-bottom-bar>
    <success-to-cart :class="{active:isActive}"></success-to-cart>
  </div>
</template>

<script>
import DetailNavBar from "./childcomp/DetailNavBar";
import DetailSwiper from "./childcomp/DetailSwiper";
import DetailGoodsInfo from "./childcomp/DetailGoodsInfo";
import DetailShopInfo from "./childcomp/DetailShopInfo";
import DetailImageInfo from "./childcomp/DetailImageInfo";
import DetailParams from "./childcomp/DetailParams";
import DetailComment from './childcomp/DetailComment';
import DetailBottomBar from './childcomp/DetailBottomBar'

import Scroll from "@/components/common/scroll/Scroll";
import GoodsList from '@/components/content/goodslist/GoodsList'
import BackTop from '@/components/common/backtop/BackTop'
import SuccessToCart from '@/components/common/successtocart/SuccessToCart'

import { getDetail,goods,getRecommend } from "@/network/detail";

import debounce from "@/common/debounce";

export default {
  name: "Detail",
  components: {
    DetailNavBar,
    DetailSwiper,
    DetailGoodsInfo,
    DetailShopInfo,
    DetailImageInfo,
    DetailParams,
    DetailComment,
    DetailBottomBar,
    Scroll,
    GoodsList,
    BackTop,
    SuccessToCart
  },
  data() {
    return {
      iid: null,
      topimages: [],
      goods: {},
      shop: {},
      detailimages: {},
      params: {},
      comment: {},
      recommend: [],
      scrollPosition: [],
      positionY: 0,
      currentIndex: 0,
      iscurrent: false,
      isActive: true
    };
  },
  created() {
    //获取商品iid
    this.iid = this.$route.params.iid;

    //获取detail数据
    getDetail(this.iid).then(res => {
      console.log(res);
      const data = res.data.result;
      //拿轮播图
      this.topimages = data.itemInfo.topImages;
      //拿商品价格信息
      this.goods = new goods(
        data.itemInfo,
        data.columns,
        data.shopInfo.services
      );
      //拿店铺信息
      this.shop = data.shopInfo;
      //拿商品详情图片
      this.detailimages = data.detailInfo;
      //拿商品参数信息
      this.params = data.itemParams;
      //拿评论信息
      if(data.rate && data.rate.list){
        this.comment = data.rate.list[0];
      }
    });

    //获取推荐数据
    getRecommend().then(res => {
      // console.log(res)
      this.recommend = res.data.data.list;
    });    
  },
  mounted() {  
    const refresh = debounce(this.$refs.scroll.refresh,200);
    //计算顶部导航栏跳转距离 
    const getPosition = debounce(() => {
      if(this.$refs.params && this.$refs.comment && this.$refs.recommend){
        this.scrollPosition = [];
        this.scrollPosition.push(0);
        this.scrollPosition.push(this.$refs.params.$el.offsetTop -44);
        this.scrollPosition.push(this.$refs.comment.$el.offsetTop - 44);
        this.scrollPosition.push(this.$refs.recommend.$el.offsetTop - 44);
        //添加一个，方便后面滚动联动时判断
        this.scrollPosition.push(Number.MAX_VALUE);
      }      
    },200)
    this.$bus.$on('detailImgLoad',() => {
      refresh();
      getPosition();
    })
  },
  methods: {
    titleClick(index) {
      // console.log(index)
      this.$refs.scroll.scrollTo(0,-this.scrollPosition[index],200)
    },
    scroll(position) {
      this.positionY = -position.y;
      this.iscurrent = this.positionY > 1000;
      for(let i = 0;i < this.scrollPosition.length-1; i++) {
        if(this.currentIndex !== i && (this.positionY >= this.scrollPosition[i] && this.positionY < this.scrollPosition[i+1])) {
          this.currentIndex = i;
          this.$refs.nav.currentIndex = this.currentIndex;
        }
      }
    },
    backClick() {
      this.$refs.scroll.scrollTo(0,0,200)
    },
    addToCart() {
      let product = {};
      product.iid = this.iid;
      product.image = this.topimages[0];
      product.title = this.goods.title;
      product.desc = this.goods.desc;
      product.price = this.goods.price;
      this.$store.dispatch('addCart',product);

      this.isActive = false;
      setTimeout(() => {
      this.isActive = true;
      },650)
    }
  }
};
</script>

<style scoped>
.detail {
  position: relative;
  z-index: 9;
  background-color: #fff;
  height: 100vh;
  overflow: hidden;
}

.nav-bar {
  position: relative;
  z-index: 10;
  background-color: #fff;
}

.scroll {
  height: calc(100vh - 44px - 49px);
}

.active {
  visibility: hidden;
}
</style>